package com.streamer.app

import android.os.Bundle
import android.view.View
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.SwitchCompat
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.adapter.GiftRankAdapter
import com.streamer.app.utils.api.ViewModel

class GiftEffectActivity : BaseActivity() {
    private var viewModel = ViewModel()
    private lateinit var clTopBar: ConstraintLayout
    private lateinit var rvRank: RecyclerView
    private lateinit var switchEffect: SwitchCompat
    private lateinit var switchSwitchBlockSpecialEffects: SwitchCompat
    private lateinit var tvTabEffect: TextView
    private lateinit var tvTabClose: TextView
    private var mRoomId = ""
    private val prefs by lazy { getSharedPreferences("switch_prefs", MODE_PRIVATE) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mRoomId = intent.getStringExtra("roomId").toString()

        viewModel.getLiveInfo()

        setContentView(R.layout.activity_gift_effect)
        clTopBar = findViewById(R.id.cl_top_bar)
        rvRank = findViewById(R.id.rv_gift_rank)
        switchSwitchBlockSpecialEffects = findViewById(R.id.switch_block_special_effects)
        switchEffect = findViewById(R.id.switch_effect)
        tvTabEffect = findViewById(R.id.tv_tab_effect)
        tvTabClose = findViewById(R.id.tv_tab_close)
        findViewById<View>(R.id.iv_back).setOnClickListener { finish() }
        setupTopPadding()
        setupRecycler()
        viewModel.LiveInfoLV.observe(this@GiftEffectActivity) {
            if (it != null) {
//                    是否开启特效
                // 恢复状态
                switchSwitchBlockSpecialEffects.isChecked = it.isSpecialEffectsEnabled


//                    是否关闭打赏
                switchEffect.isChecked = !it.isActiveteTippingEnabled

            }
        }
        switchSwitchBlockSpecialEffects.setOnCheckedChangeListener { _, isChecked ->
            viewModel.closeOpenGiftEffect()

        }

        switchEffect.setOnCheckedChangeListener { _, isChecked ->

            viewModel.closeOpenTipping()

        }
        viewModel.anchorPage(99, 1, mRoomId)
        viewModel.AnchorLV.observe(this@GiftEffectActivity) {
            if (it != null) {
                rvRank.layoutManager = LinearLayoutManager(this)

                rvRank.adapter = it.rows?.let { items -> GiftRankAdapter(items) }
            }
        }

    }

    private fun setupTopPadding() {
        ViewCompat.setOnApplyWindowInsetsListener(clTopBar) { view, insets ->
            val statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top
            view.setPadding(
                view.paddingLeft, statusBarHeight, view.paddingRight, view.paddingBottom
            )
            insets
        }
    }

    private fun setupRecycler() {

    }
}
